﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


using System.Runtime.InteropServices;
using System.Xml;
using System.IO;
 

namespace ForExam
{
    public partial class MainForm : Form
    {
        #region 按钮名称
        //信息系统项目管理师	    Information Systems Project Management Division 	ISPMD
        //系统分析师	            Systems Analyst 	                                S_ANA
        //软件设计师	            Software Architect 	                                S_ARC
        //网络工程师	            Network Engineer 	                                N_E
        //信息系统监理师	        Information Systems Supervision Division 	        ISSD
        //系统集成项目管理工程师	System Integration Project Management Engineer  	SIPME
        //信息系统管理工程师	    Information Systems Management Engineer 	        ISME
        //数据库系统工程师	        Database Systems Engineer                       	DSE
        //多媒体应用设计师	        Multimedia Application Designer                 	MAD
        //程序员	                Programmer 	                                        Programmer 
        //网络管理员	            Network administrator 	                            NET_ADMIN
        //信息处理技术员	        Information Processing Technician	                IPT
        #endregion
        #region 无边框窗体拖动
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        #endregion

        public MainForm()
        {
            InitializeComponent();
        }
        //关闭窗口
        private void tslClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        //最小化
        private void tslMin_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        //去掉toolstrip的白边
        private void toolStrip1_Paint(object sender, PaintEventArgs e)
        {
            if ((sender as ToolStrip).RenderMode != ToolStripRenderMode.System)
                return;
            Rectangle rect = new Rectangle(0, 0, this.toolStrip1.Width - 2, this.toolStrip1.Height - 8);
            e.Graphics.SetClip(rect);
        }

        //系统集成项目管理工程师
        private void btn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn == null)
                return;
            //根据按钮名字查找项目
            if (m_LstExamination.Count<=0)
            {
                CommondMethod.OutLog("配置文件为空");
                return;
            }
            string strExamName = btn.Name.Substring(3);
            var Exam = m_LstExamination.Find(a=>string.Compare( a.shortname,strExamName,true)==0);
            if (Exam == null)
            {
                CommondMethod.OutLog("未找到相关考试项目");
                return;
            }
            string strExamPath = m_DataDir + Exam.FileName;
            if (!File.Exists(strExamPath))
            {
                CommondMethod.OutLog("考试文件缺失");
                return;
            }
            ShowTopic st = new ShowTopic(strExamPath);
            st.Show();
        }
        string m_ConfigName = "config.xml";
        string m_DataDir;
        List<Examination> m_LstExamination = new List<Examination>();
        private void MainForm_Load(object sender, EventArgs e)
        {
            string str = Environment.CurrentDirectory;
            DirectoryInfo pDirInfo = new DirectoryInfo(Environment.CurrentDirectory);
            m_DataDir = pDirInfo.Parent.Parent.FullName + "\\Data\\";
            if (!Directory.Exists(m_DataDir))
            {
                CommondMethod.OutLog("文件缺失");
                return;
            }
            string strConfigPath = m_DataDir + m_ConfigName;
            if (!File.Exists(strConfigPath))
            {
                CommondMethod.OutLog("配置文件缺失");
                return;
            }
            //读取配置文件
            XmlDocument pDoc = new XmlDocument();
            pDoc.Load(strConfigPath);
            XmlNode pRootNode = pDoc.SelectSingleNode("Examinations");
            foreach (XmlElement ele in pRootNode.ChildNodes)
            {
                Examination pExam = new Examination();
                pExam.Discription = ele.GetAttribute("info");
                pExam.Level = ele.GetAttribute("Level");
                pExam.shortname = ele.GetAttribute("shortname");
                pExam.FileName = ele.InnerText + ".xml";
                if (!m_LstExamination.Contains(pExam))
                    m_LstExamination.Add(pExam);
            }
        }
    }
}
